package cn.good.yan.b11;

import java.util.Arrays;

/**
 * b11  比较器 Comparable
 *
 * @author shengren.yan
 * @create 2023-07-30
 */
public class Test {


    public static void main(String[] args) {
        // 排序
        TPojo[] arr = new TPojo[5];
        arr[0] = new TPojo("a王",1);
        arr[1] = new TPojo("a刘",3);
        arr[2] = new TPojo("b时",83);
        arr[3] = new TPojo("e物",73);
        arr[4] = new TPojo("c活",13);
        Arrays.sort(arr);
        System.out.println(arr);

        // 方式2 的排序
        Arrays.sort(arr, (a, b) -> {
            // 先比age ，再比较name
            int compare = Integer.compare(a.getAge(), b.getAge());
            if (compare != 0) {
                return compare;    //  (从小到大)
//                return - compare; // (从大到小)
            }
            return a.getName().compareTo(a.getName());
        });
        System.out.println(arr);


        // 直接比较大小
        TPojo a1 = new TPojo("a王", 1);
        TPojo a2 = new TPojo("a王", 1);
        int compareTo = a1.compareTo(a2);
        if (compareTo > 0) {
            System.out.println("a1大");
        } else if (compareTo < 0) {
            System.out.println("a2大");
        } else {
            System.out.println("一样大");
        }

    }

}
